Skip to content

RegisterEvent anonymous functions implementation (Additionally prevent CreateEventTimer() to register the same event more than one time)#474

Open
boubouleuh wants to merge 14 commits intoBeamMP:minorfrom
boubouleuh:anonymous_functions
Open

RegisterEvent anonymous functions implementation (Additionally prevent CreateEventTimer() to register the same event more than one time)#474
boubouleuh wants to merge 14 commits intoBeamMP:minorfrom
boubouleuh:anonymous_functions

Conversation

@boubouleuh
Copy link
Copy Markdown
Contributor

@boubouleuh boubouleuh commented Feb 23, 2026

This PR implements the usage of the function directly inside RegisterEvent() while keeping the possibility of using the function name.

MP.RegisterEvent("Test",function()
Util.LogInfo("TEST")
end)
MP.TriggerLocalEvent("Test")

This needed A LOT of changes; since I'm still a beginner in C++, there are probably a couple of issues that I would be happy to fix (can't imagine that there aren't any).

If this gets merged, it would be better to merge it before #473 to prevent conflicts regarding the DontCallOnInit variable that isn't completely removed in this PR but is in #473.

ofc, I tested every changeand it seemed to work well (im just not sure about the queued lua console cmd)

Closes #449
Closes #441 the panichandler is fixed in this PR

By creating this pull request, I understand that code that is AI generated or otherwise automatically generated may be rejected without further discussion.
I declare that I fully understand all code I pushed into this PR, and wrote all this code myself and own the rights to this code.

…FromCustomEvent that was crashing the server on script reload, mStateFunctionQueue is now a deque

additionally createEventTimer cant register the same event more than one time now
@boubouleuh boubouleuh changed the title RegisterEvent nonymous functions implementation RegisterEvent anonymous functions implementation Feb 23, 2026
@boubouleuh boubouleuh changed the title RegisterEvent anonymous functions implementation RegisterEvent anonymous functions implementation (Additionally prevent CreateEventTimer() to register the same event more than one time) Feb 23, 2026
@boubouleuh
Copy link
Copy Markdown
Contributor Author

When trying to add more than one event timer it break tho idk how to fix 🫡

@boubouleuh
Copy link
Copy Markdown
Contributor Author

seems like i found the issues now it doesnt crash anymore

@WiserTixx
Copy link
Copy Markdown
Collaborator

I've been testing this for a little bit, and it seems like registering more than 3 of the same events causes an error when you try to call them.
image

@boubouleuh
Copy link
Copy Markdown
Contributor Author

I've been testing this for a little bit, and it seems like registering more than 3 of the same events causes an error when you try to call them. image

im looking into it, however interestingly it work if its triggered with an event timer

Fixed stack exceptions and lua panic inside StateThreadData::operator()()
@boubouleuh
Copy link
Copy Markdown
Contributor Author

took me alot of time to debug everything that needed to be debugged but i think its good now, also i now use a shared_ptr for LuaFunction to prevent issues with the lua reference and it seems to work, and the events are properly cleared on hotreload now

Copy link
Copy Markdown
Collaborator

@SaltySnail SaltySnail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, small changes requested.

Copy link
Copy Markdown
Contributor

@lionkor lionkor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look since I was looking for the stability improvements, and saw a couple things. Hope it's okay :)

@lionkor
Copy link
Copy Markdown
Contributor

lionkor commented Apr 10, 2026

Also the vector to dequeue change should only make it faster if we pop from the front, if we pop from the back, we don't need to memcpy and it shouldn't make a difference which one is used.

@boubouleuh
Copy link
Copy Markdown
Contributor Author

boubouleuh commented Apr 10, 2026

Also the vector to dequeue change should only make it faster if we pop from the front, if we pop from the back, we don't need to memcpy and it shouldn't make a difference which one is used.

yes its better because its doing alot of mStateFunctionQueue.pop_front(); i even compared the performance for both

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support anonymous functions and multiple handlers in MP.RegisterEvent Lua Stack Corruption leads to x86 stack overflow

4 participants